<?php

/**
 * ECTouch Open Source Project
 * ============================================================================
 * Copyright (c) 2012-2014 http://ectouch.cn All rights reserved.
 * ----------------------------------------------------------------------------
 * 文件名称：IndexController.class.php
 * ----------------------------------------------------------------------------
 * 功能描述：ECTouch首页控制器
 * ----------------------------------------------------------------------------
 * Licensed ( http://www.ectouch.cn/docs/license.txt )
 * ----------------------------------------------------------------------------
 */
/* 访问控制 */
defined('IN_ECTOUCH') or die('Deny Access');

class IndexController extends CommonController {

    /**
     * 首页信息
     */
    public function index() {
        $cache_id = sprintf('%X', crc32($_SESSION['user_rank'] . '-' . C('lang')));
        if (!ECTouch::view()->is_cached('index.dwt', $cache_id))
        {
            // 自定义导航栏
            $navigator = model('Common')->get_navigator();
            $this->assign('navigator', $navigator['middle']);
            $ad = M()->table('ad')->where(['position_id'=>255])->field('ad_code')->select();
            foreach ($ad as &$value)
            {
                $value['ad_code'] = (strpos($value['ad_code'], 'http://') === false && strpos($value['ad_code'], 'https://') === false) ?
                    get_data_path($value['ad_code'], 'afficheimg') : $value['ad_code'];
            }
            $this->assign('first_banner',$ad[0]);
            $this->assign('banner',$ad);
            $this->assign('last_banner',$ad[count($ad) - 1]);

            $best_goods = model('Index')->goods_list('best', C('page_size'));//促销
            $new_goods = model('Index')->goods_list('new', C('page_size'));//新品
            $temp_hot = model('Index')->goods_list('hot', C('page_size'));//热销

            $hot_goods = [];
            $index = 0;
            $temp = [];
            foreach ($temp_hot as $value)
            {
                $temp[] = $value;
                $index ++;
                if($index == 2)
                {
                    $hot_goods[] = $temp;
                    $temp = [];
                    $index = 0;
                }
            }
            if(count($temp) == 1)
            {
                $hot_goods[] = $temp;
            }
            $this->assign('first_hot', $hot_goods[0]);
            $this->assign('last_hot', $hot_goods[count($hot_goods)-1]);
            $this->assign('hot_total', count($hot_goods));

            $this->assign('best_goods', $best_goods);
            $this->assign('new_goods', $new_goods);
            $this->assign('hot_goods', $hot_goods);
        }
        $this->display('index.dwt');
    }

    /**
     * ajax获取商品
     */
    public function ajax_goods() {
        if (IS_AJAX) {
            $type = I('get.type');
            $start = $_POST['last'];
            $limit = $_POST['amount'];
            $goods_list = model('Index')->goods_list($type, $limit, $start);
            $list = array();
            // 热卖商品
            if ($goods_list) {
                foreach ($goods_list as $key => $value) {
                    $value['iteration'] = $key + 1;
                    $this->assign('goods', $value);
                    $list [] = array(
                        'single_item' => ECTouch::view()->fetch('library/asynclist_index.lbi')
                    );
                }
            }
            echo json_encode($list);
            exit();
        } else {
            $this->redirect(url('index'));
        }
    }

}
